{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 二维数组创建向量\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码首先利用 NumPy 库创建了一个二维行向量，并通过各种索引和切片操作对其进行数学上的处理。具体来说，代码定义了\n",
    "\n",
    "$$\n",
    "row\\_vector = \\begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\end{bmatrix},\n",
    "$$\n",
    "\n",
    "其中 $row\\_vector$ 是一个形状为 $(1,10)$ 的二维数组。通过属性 $ndim$ 和 $shape$ 分别确认该数组有 2 个维度和 1 行 10 列。\n",
    "\n",
    "接下来，代码使用索引操作，如 $row\\_vector[0,0]$ 访问第一个元素（$1$），$row\\_vector[0,1]$ 访问第二个元素（$2$），以及 $row\\_vector[0,-1]$ 访问最后一个元素（$10$）。切片操作则用于提取子数组，例如：\n",
    "\n",
    "$$\n",
    "row\\_vector[:, :2] \\quad \\text{提取} \\quad [1,2],\n",
    "$$\n",
    "\n",
    "$$\n",
    "row\\_vector[:, 1:] \\quad \\text{提取} \\quad [2,3,4,5,6,7,8,9,10],\n",
    "$$\n",
    "\n",
    "$$\n",
    "row\\_vector[:, -3:] \\quad \\text{提取} \\quad [8,9,10].\n",
    "$$\n",
    "\n",
    "随后，通过转置操作 $T$ 将行向量转换为列向量，得到\n",
    "\n",
    "$$\n",
    "col\\_vector = row\\_vector^T = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\\\ 4 \\\\ 5 \\\\ 6 \\\\ 7 \\\\ 8 \\\\ 9 \\\\ 10 \\end{bmatrix}.\n",
    "$$\n",
    "\n",
    "对列向量的索引操作，如 $col\\_vector[0,0]$、$col\\_vector[1,0]$ 和 $col\\_vector[-1,0]$ 分别返回 $1$、$2$ 和 $10$。而切片操作 $col\\_vector[:2, :]$、$col\\_vector[1:, :]$ 和 $col\\_vector[-3:, :]$ 则提取出列向量的前两行、从第二行开始的所有行以及最后三行。这些操作与矩阵中选取子矩阵的方法类似，展示了如何用编程实现数学中的向量与矩阵操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a836d0c-40a8-4621-b082-3bf21a316632",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入 NumPy 库，用于数组操作和数值计算\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8868ebe3-f69f-43a3-b735-3940aecbe755",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector = [[ 1  2  3  4  5  6  7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 创建一个行向量（二维数组）：该数组有 1 行 10 列\n",
    "row_vector = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])\n",
    "# 输出行向量，数学上表示为 r = [1, 2, 3, ..., 10]\n",
    "print(\"row_vector =\", row_vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5cacc4b4-a7f7-4ad5-b328-bde21c3c7a9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector.ndim = 2\n"
     ]
    }
   ],
   "source": [
    "# 获取数组的维数，二维数组的维数为 2（即存在行和列两个轴）\n",
    "print(\"row_vector.ndim =\", row_vector.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "26a8e24f-90cc-402d-9e73-6da41838a1f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector.shape = (1, 10)\n"
     ]
    }
   ],
   "source": [
    "# 获取数组的形状，返回一个元组，此处为 (1, 10) 表示 1 行 10 列\n",
    "print(\"row_vector.shape =\", row_vector.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de825bc7-5bb3-495b-bfae-44c9946147e4",
   "metadata": {},
   "source": [
    "### 索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3b98c3b3-e97a-4021-aa5e-4f525968ffa9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[0, 0] = 1\n"
     ]
    }
   ],
   "source": [
    "# 访问行向量的第一个元素，索引 [0, 0] 表示第一行第一列，结果为 1\n",
    "print(\"row_vector[0, 0] =\", row_vector[0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2569443c-3a62-4c2a-9801-010351393da5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[0, 1] = 2\n"
     ]
    }
   ],
   "source": [
    "# 访问行向量的第二个元素，索引 [0, 1] 表示第一行第二列，结果为 2\n",
    "print(\"row_vector[0, 1] =\", row_vector[0, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "042a8eca-e7d7-4b55-99fa-0af0a1072523",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[0, -1] = 10\n"
     ]
    }
   ],
   "source": [
    "# 访问行向量的最后一个元素，索引 [0, -1] 中 -1 表示最后一列，结果为 10\n",
    "print(\"row_vector[0, -1] =\", row_vector[0, -1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48e3bcb8-2747-4675-b064-169dfa0e4c85",
   "metadata": {},
   "source": [
    "### 切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1b230dd7-35f8-43d2-899a-8b1615f9c8e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[:, :2] = [[1 2]]\n"
     ]
    }
   ],
   "source": [
    "# 提取所有行中前两列的元素（索引范围 [0,2)），得到 [1, 2]\n",
    "print(\"row_vector[:, :2] =\", row_vector[:, :2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "26900973-caf9-4c69-a0ae-63b5fd3e0d9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_vector[:, :2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8cb292ce-380a-4999-9f45-92c2bafe45c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[:, 1:] = [[ 2  3  4  5  6  7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 提取所有行中从第二列开始到最后一列的元素（索引范围 [1, end)），得到 [2, 3, ..., 10]\n",
    "print(\"row_vector[:, 1:] =\", row_vector[:, 1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8c350d6d-1c6b-45e5-9558-3ea97773bd1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "row_vector[:, -3:] = [[ 8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 提取所有行中最后三列的元素（索引范围 [-3, end)），得到 [8, 9, 10]\n",
    "print(\"row_vector[:, -3:] =\", row_vector[:, -3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd14ce5a-1b39-4c1a-902f-ae184b33368c",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 创建列向量 (二维)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "32c9c6a0-2f79-4d4c-9e7b-6322a3efdc3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 转置操作：将行向量转置为列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f8f6e9c3-15ee-4473-b972-7b4aa9a68810",
   "metadata": {},
   "outputs": [],
   "source": [
    "col_vector = row_vector.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4e603b30-af67-4618-925d-c40d4f6df253",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector = [[ 1]\n",
      " [ 2]\n",
      " [ 3]\n",
      " [ 4]\n",
      " [ 5]\n",
      " [ 6]\n",
      " [ 7]\n",
      " [ 8]\n",
      " [ 9]\n",
      " [10]]\n"
     ]
    }
   ],
   "source": [
    "# 输出转置后的列向量，数学上表示为\n",
    "# c = [1, 2, 3, ..., 10]^T = \n",
    "#     [1]\n",
    "#     [2]\n",
    "#     [3]\n",
    "#     ...\n",
    "#     [10]\n",
    "print(\"col_vector =\", col_vector)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce72c555-799e-4db8-a4bd-e8a79f9e8760",
   "metadata": {},
   "source": [
    "### 索引操作（针对列向量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5c0250c6-8224-4455-94ca-45939dd38b7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[0, 0] = 1\n"
     ]
    }
   ],
   "source": [
    "# 访问列向量的第一个元素，索引 [0, 0] 表示第一行第一列，结果为 1\n",
    "print(\"col_vector[0, 0] =\", col_vector[0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fd531b27-346c-4698-b913-d20011aabab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[1, 0] = 2\n"
     ]
    }
   ],
   "source": [
    "# 访问列向量的第二个元素，索引 [1, 0] 表示第二行第一列，结果为 2\n",
    "print(\"col_vector[1, 0] =\", col_vector[1, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "74004474-d526-43d3-8327-2c59318dd86d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[-1, 0] = 10\n"
     ]
    }
   ],
   "source": [
    "# 访问列向量的最后一个元素，索引 [-1, 0] 表示最后一行第一列，结果为 10\n",
    "print(\"col_vector[-1, 0] =\", col_vector[-1, 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00c5e504-a705-4950-bf1a-3332da035ee8",
   "metadata": {},
   "source": [
    "### 切片操作（针对列向量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "309f338f-4dbf-4c7c-9a03-a6a1f2a1db72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[:2, :] = [[1]\n",
      " [2]]\n"
     ]
    }
   ],
   "source": [
    "# 提取列向量前两行的所有列（所有元素），得到 [[1], [2]]\n",
    "print(\"col_vector[:2, :] =\", col_vector[:2, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fcc52786-d320-451e-9df0-55e8da45b4a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[1:, :] = [[ 2]\n",
      " [ 3]\n",
      " [ 4]\n",
      " [ 5]\n",
      " [ 6]\n",
      " [ 7]\n",
      " [ 8]\n",
      " [ 9]\n",
      " [10]]\n"
     ]
    }
   ],
   "source": [
    "# 提取列向量从第二行到最后一行的所有列，得到 [[2], [3], ..., [10]]\n",
    "print(\"col_vector[1:, :] =\", col_vector[1:, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1ab0594a-4f07-4b92-b0e4-1d99972ddf2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col_vector[-3:, :] = [[ 8]\n",
      " [ 9]\n",
      " [10]]\n"
     ]
    }
   ],
   "source": [
    "# 提取列向量最后三行的所有列，得到 [[8], [9], [10]]\n",
    "print(\"col_vector[-3:, :] =\", col_vector[-3:, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fce1cf41-3ad3-49df-b1ad-c61b942e143e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "be5cb877-b35d-4b55-ba32-3fac622879a3",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
